﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ToppingControl.Models;
using WinNemoLib;

namespace ToppingControl.Controllers
{
    public class ToppedCordPlanController : Controller
    {
        //
        // GET: /ToppedCordPlan/

        public ActionResult Index(string dtPlan)
        {
           if (StrUtil.IsEmpty(dtPlan)) dtPlan = WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);
           ViewBag.Message = dtPlan;
           List<TopCordPlanModel.TopCordPlan_DS> list = new List<TopCordPlanModel.TopCordPlan_DS>();
           TopCordPlanModel mdSearch = new TopCordPlanModel();
           list = mdSearch.SEL_UNLOCK(dtPlan);
           return View(list);
        }

        public JsonResult Import()
        {
           string dtPlan = Request["dtPlan"].ToString();
           if (StrUtil.IsEmpty(dtPlan)) dtPlan = WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);
           ViewBag.Message = dtPlan;
           List<TopCordPlanModel.TopCordPlan_DS> list = new List<TopCordPlanModel.TopCordPlan_DS>();
           TopCordPlanModel mdSearch = new TopCordPlanModel();
           list = mdSearch.SEL_TOPCORDPLN("",dtPlan);
           return Json(list);
        }

        public ActionResult Report()
        {
           return RedirectToAction("Index", "Table");
        }

        public ActionResult unLockTopping(string dtPlan)
        {
           ViewBag.Message = dtPlan;
           List<TopCordPlanModel.TopCordPlan_DS> list = new List<TopCordPlanModel.TopCordPlan_DS>();
           TopCordPlanModel mdSearch = new TopCordPlanModel();
           list = mdSearch.SEL_UNLOCK(dtPlan);
          
           return View(list);
        }


        public JsonResult LockAll(string dtPlan)
        {
           ViewBag.Message = dtPlan;
           TopCordPlanModel mdSearch = new TopCordPlanModel();

           if (mdSearch.UPDATE(User.Identity.Name.ToString(), "L","",dtPlan))
           {
              return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
           }
           else
              return Json(new { success = 0, message = "ไม่สามารถทำงาน" });

         
        }
        public JsonResult unLock(string sel)
        {
           string[] seqs = sel.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
           ViewBag.Message = seqs[0];
           TopCordPlanModel mdSearch = new TopCordPlanModel();

           if (mdSearch.UPDATE(User.Identity.Name.ToString(), "U",sel))
           {
              return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
           }
           else
              return Json(new { success = 0, message = "ไม่สามารถทำงาน" });


        }
       
        public JsonResult Add_Edit_UNLock(List<TopCordPlanModel.TopCordPlan_DS> theData,string dtPlan)
        {
   
           Boolean bln=true;
           string err = "";
           using (ConnectionWrapper cnn = Nemo.GetDefaultConfiguredDbConnectionWrapper())
           {
              cnn.Open();
              cnn.BeginTransaction();
              TopCordPlanModel tp = new TopCordPlanModel();

           
              int itemno  = tp.GET_SEQ(dtPlan);

              if (!tp.DEL_DATA_NO_ACTIVE(cnn,dtPlan))
              {
                 cnn.RollbackTransaction();
                 bln = false;
                 goto ExitProcess;
              }
              if (theData == null)
              {
                 cnn.CommitTransaction();
                 return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
              }

              foreach (TopCordPlanModel.TopCordPlan_DS incom in theData)
              {

                 if (StrUtil.IsNotEmpty(incom._Topcrdcd) && StrUtil.IsNotEmpty(incom._Manufc) && StrUtil.IsNotEmpty(incom._Length))
                 {

                    TopCordBomModel chk = new TopCordBomModel();
                    List<TopCordBomModel.TopCordBom_DS> list = new List<TopCordBomModel.TopCordBom_DS>();
                    list=chk.SEL_TOPCORDBOM(incom._Topcrdcd);
                    if (list.Count()<=0)
                    {
                        return Json(new { success = 0, message = "ไม่พบข้อมูลในระบบ "+ incom._Topcrdcd });
                    }
                    incom._Itemno = itemno.ToString();
                    incom._Plants = (StrUtil.IsEmpty(incom._Plants) ? "U" : incom._Plants);
                    if (!tp.Ins_Data(cnn, incom,User.Identity.Name.ToString()))
                    {
                       cnn.RollbackTransaction();
                       bln = false;
                       err = incom._Plandt;
                       goto ExitProcess;
                    }
                    itemno = itemno + 10;
                 }
              }
              cnn.CommitTransaction();
           }
           
           ExitProcess :
           
 
           if (bln)
           {
              return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
           }
           else
              return Json(new { success = 0, message = err });

        }

        public JsonResult GetCUSTOMER()
        {
           TopCordBomModel cus = new TopCordBomModel();
           string[] s = cus.SEL_CUSTOMER();
           return Json(s);
         //   string[] s = new string[] {  "TOYOTA",                 
         //         "HONDA",
         //         "ISUZU",
         //         "SUZUKI",
         //         "BENZ"
         //};
           //return Json(s);
        }

        public JsonResult Get_USE_ACTIVE()
        {
           List<TopCordPlanModel.TopCordPlan_DS> list = new List<TopCordPlanModel.TopCordPlan_DS>();
           TopCordPlanModel mdSearch = new TopCordPlanModel();
           //WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN)
           list = mdSearch.GET_TOPCORDPLN_ACT("","15",true);           
           return Json(list);
        }



    }
}
